package cn.com.kh;

import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class GetURLStatusCode {
	private static final Log log = LogFactory.getLog(GetURLStatusCode.class);

	public static void main(String[] args) {

		System.out.println(get("http://117.179.147.162:9011/ngs/fs/get?file=6f5ec213-d96c-459d-a409-9e4f958ceda9.html"));

	}

	private static int get(String urlString) {
		log.debug("检查目标资源地址:" + urlString);
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet(urlString);
		RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(3000).setSocketTimeout(5000).build();
		httpGet.setConfig(requestConfig);
		CloseableHttpResponse httpGetResponse;
		int StatusCode = 500;
		try {
			httpGetResponse = httpClient.execute(httpGet);
			StatusCode = httpGetResponse.getStatusLine().getStatusCode();
		} catch (ClientProtocolException e) {
			log.error("检查目标资源ClientProtocolException ", e);
		} catch (IOException e) {
			log.error("检查目标资源IOException ", e);
		} catch (Exception e) {
			log.error("检查目标资源Exception ", e);
		}

		log.debug("检查目标资源结果:" + StatusCode);
		return StatusCode;
	}

}
